package com.ruoyi.jinchuguanli.controller;

import com.ruoyi.cangku.controller.tiaoMaAuto;
import com.ruoyi.cangku.mapper.ShangpinguanliMapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gongjulei;
import com.ruoyi.jinchuguanli.domain.Shangpin;
import com.ruoyi.jinchuguanli.domain.Xiaoshouchuku;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

@Component
public class chuKuDanUpload {

    private static String env;

    @Value("${ruoyi.profile}")
    public void setEnv (String env) {
        chuKuDanUpload.env = env;
    }

    public static String img2New(Xiaoshouchuku xiaoshouChuku, ShangpinguanliMapper shangpinguanliMapper) {
        int width = 905; // 图片宽
        int height = 343;// 图片高
        String titleStr = "现货仓发货通知单";
        String fahuodanhaoName = "发货单号";
        String fahuodanhao = xiaoshouChuku.getChukudanhao();
        String xiangmumingName = "项目名称";
        String xiangmumingcheng = xiaoshouChuku.getXiangmumingcheng();
        String yewuyuanName = "业务员";
        String yewuyuan = xiaoshouChuku.getYewuyuan();
        String fahuorenName = "发货人";
        String fahuoren = xiaoshouChuku.getFahuoren();
        String riqiName = "日期";
        String riqi = new SimpleDateFormat("yyyy.MM.dd").format(xiaoshouChuku.getXiadanriqi());
        String shouhuodizhiName = "收货地址";
        String shouhuodizhi = xiaoshouChuku.getShouhuodizhi();
        String xiaoshoueName = "销售额 元";
        String xiaoshoue = String.valueOf(xiaoshouChuku.getZongjine());
        String pinmingName = "品名", chicunyukaixiangName = "产品尺寸及开向", chanpinshuliangName = "产品数量", chanpinbeizhuName = "产品备注";
        String menming = "";
        String fanghuosuo = "防火锁", bimenqi = "闭门器", labaogai = "拉爆盖";
        String menshsangao = "0", menshankuan = "0", menkaixiang = null, menshanshu = "0", menbeizhu = null;
        String wujin1shu = "0", wujin2shu = "0", wujin3shu = "0";
        List<Shangpin> shangpinList = gongjulei.aReturnFormat(xiaoshouChuku,shangpinguanliMapper).getShangpin();
        if (shangpinList == null) {
            return null;
        }
        for (Shangpin s : shangpinList
        ) {
            if (!s.getShangpinleibie().equals("防火锁") && !s.getShangpinleibie().equals("闭门器") && !s.getShangpinleibie().equals("拉爆盖")) {
                menming = s.getShangpinming();
                menshankuan = String.valueOf(s.getShangpinguigekuan());
                menshsangao = String.valueOf(s.getShangpinguigegao());
                menkaixiang = s.getShangpinkaixiang();
                menshanshu = String.valueOf(s.getShangpinshuliang());
                menbeizhu = xiaoshouChuku.getBeizhu();
            } else if (s.getShangpinleibie().equals("防火锁")) {
                wujin1shu = String.valueOf(s.getShangpinshuliang());
            } else if (s.getShangpinleibie().equals("闭门器")) {
                wujin2shu = String.valueOf(s.getShangpinshuliang());
            } else if (s.getShangpinleibie().equals("拉爆盖")) {
                wujin3shu = String.valueOf(s.getShangpinshuliang());
            }
        }

        int outSize4Height = 0;
        String dizhi1 = null,dizhi2 = null;

//        if (menming != null) {
//            menshsangao = String.valueOf(xiaoshouChuku.getMen1shanGao());
//            menshankuan = String.valueOf(xiaoshouChuku.getMen1shanKuan());
//            menkaixiang = xiaoshouChuku.getKaiXiang1();
//            menshanshu = String.valueOf(xiaoshouChuku.getMen1Shuliang());
//        }
//        String menkuanshu = String.valueOf(xiaoshouChuku.getKuangShuliang());


        // 得到图片缓冲区
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// INT精确度达到一定,RGB三原色，高度70,宽度150

        // 得到它的绘制环境(这张图片的笔)
        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        Graphics2D g3 = (Graphics2D) bi.getGraphics();
        g3.setFont(new Font("宋体", Font.BOLD, 18));
        int[] lengthSize = tiaoMaAuto.sizeCheckAndReturn(g3,shouhuodizhi);
        if (lengthSize[1] > (630-130)) {
            int outSize = lengthSize[1]-(630-130);
            double outFont = Math.floor((double) lengthSize[0] * ((double) outSize / (double) lengthSize[1]));

            if (outFont != ((double) lengthSize[0] * ((double) outSize / (double) lengthSize[1]))) {
                outFont++;
            }
            int inFont = (int) (lengthSize[0]-outFont);
            dizhi1 = shouhuodizhi.substring(0, inFont);
            dizhi2 = shouhuodizhi.substring(inFont);
            outSize4Height = lengthSize[2]+2;
            bi = new BufferedImage(width,height+outSize4Height, BufferedImage.TYPE_INT_RGB);
            bi.flush();
            g2 = (Graphics2D) bi.getGraphics();
        }
        g2.setColor(Color.WHITE); // 设置背景颜色
        g2.fillRect(0, 0, width, height+outSize4Height);// 填充整张图片(其实就是设置背景颜色)
        g2.setColor(Color.black);// 设置字体颜色
        g2.setStroke(new BasicStroke(2.0f)); // 边框加粗
        g2.drawRect(1, 1, width - 2, height+outSize4Height - 2); // 画边框就是黑边框

        g2.drawLine(0, 37, 905, 37); // 从上到下第二个横线(标题下面横线)
        g2.setStroke(new BasicStroke(0.0f)); // 边框不需要加粗
        g2.drawLine(0, 76, 905, 76); // 从上到下第三个横线(账单周期下面横线)
        g2.drawLine(0, 114, 905, 114); // 从上到下第四个横线(账单天数下面横线)
        g2.drawLine(0, 152+outSize4Height, 905, 152+outSize4Height); // 从上到下第5个横线(泵额定功率下面横线)
        g2.drawLine(0, 190+outSize4Height, 905, 190+outSize4Height); // 从上到下第6个横线(泵变频节能运行用电量下面横线)
        g2.drawLine(0, 228+outSize4Height, 905, 228+outSize4Height); // 从上到下第7个横线(电价下面横线)
        g2.drawLine(0, 266+outSize4Height, 905, 266+outSize4Height); // 从上到下第8个横线(平均节能率下面横线)
        g2.drawLine(0, 304+outSize4Height, 905, 304+outSize4Height);// 从上到下第9个横线(平均节能率下面横线)

        g2.drawLine(122, 38, 122, height+outSize4Height); // 从左到右第二个竖线
        g2.drawLine(224, 190+outSize4Height, 224, height+outSize4Height); // 从左到右第三个竖线
        g2.drawLine(240, 190+outSize4Height, 240, height+outSize4Height); // 从左到右第四个竖线
        g2.drawLine(320, 38, 320, 114); // 从左到右第5个竖线
        g2.drawLine(320, 190+outSize4Height, 320, height+outSize4Height); // 从左到右第5个竖线
        g2.drawLine(490, 38, 490, 114); // 从左到右第6个竖线
        g2.drawLine(490, 152+outSize4Height, 490, height+outSize4Height); // 从左到右第6个竖线
        g2.drawLine(630, 38, 630, height+outSize4Height); // 从左到右第7个竖线
        g2.drawLine(720, 76, 720, 152+outSize4Height); // 从左到右第8个竖线

        // 设置标题的字体,字号,大小
        Font titleFont = new Font("宋体", Font.BOLD, 30);
        g2.setFont(titleFont);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 抗锯齿
        // 计算文字长度,计算居中的X点坐标
        FontMetrics fm = g2.getFontMetrics(titleFont);
        int titleWidth = fm.stringWidth(titleStr);
        int titleWidthX = (width - titleWidth) / 2;// 感觉不居中,向左移动35个单位
        g2.drawString(titleStr, titleWidthX, 30);

        //发货单号
        g2.setFont(new Font("宋体", Font.BOLD, 18));
        g2.drawString(fahuodanhaoName, 15, 65);
        // 发货单号的值
        g2.drawString(fahuodanhao, 130, 65);

        //发货单号
        g2.drawString(xiangmumingName, 520, 65);
        // 发货单号的值
        g2.drawString(xiangmumingcheng, 635, 65);

        //发货单号
        g2.drawString(yewuyuanName, 30, 100);
        // 发货单号的值
        g2.drawString(yewuyuan, 185, 100);


        //发货单号
        g2.drawString(fahuorenName, 370, 100);
        // 发货单号的值
        g2.drawString(fahuoren, 525, 100);
        //发货单号
        g2.drawString(riqiName, 654, 103);
        // 发货单号的值
        g2.drawString(riqi, 735, 100);

        //发货单号
        g2.drawString(shouhuodizhiName, 17, 140);
        // 发货单号的值
        g2.setFont(new Font("宋体", Font.BOLD, 18));

//        g2.drawString();
        if (dizhi1 == null) {
            g2.drawString(shouhuodizhi, 130, 140);
        }else {
            g2.drawString(dizhi1, 130, 140);
            g2.drawString(dizhi2,130,140+outSize4Height);
        }

        //发货单号
        g2.setFont(new Font("宋体", Font.BOLD, 18));
        g2.drawString(xiaoshoueName, 632, 140);
        // 发货单号的值
        g2.drawString(xiaoshoue, 735, 140);

        //产品详细
        g2.drawString(pinmingName, 37, 178+outSize4Height);
        g2.drawString(chicunyukaixiangName, 230, 178+outSize4Height);
        g2.drawString(chanpinshuliangName, 522, 178+outSize4Height);
        g2.drawString(chanpinbeizhuName, 725, 178+outSize4Height);

        //门详细
//        if (menming == null) {
//            g2.drawString(kuangming, 7, 215);
//            g2.drawString(xiaoshouChuku.getKuangKuan(), 180, 215);
//            g2.drawString("*", 228, 215);
//            g2.drawString(xiaoshouChuku.getKuangGao(), 245, 215);
//            g2.drawString(xiaoshouChuku.getKaiXiang2(), 330, 215);
//            g2.drawString(menkuanshu, 550, 215);
//        }else {
        g2.drawString(menming, 7, 215+outSize4Height);
        g2.drawString(menshankuan, 180, 215+outSize4Height);
        g2.drawString("*", 228, 215+outSize4Height);
        g2.drawString(menshsangao, 245, 215+outSize4Height);
        if (StringUtils.isNotEmpty(menkaixiang)) {
            g2.drawString(menkaixiang, 330, 215+outSize4Height);
        }
        g2.drawString(menshanshu, 550, 215+outSize4Height);
        if (StringUtils.isNotEmpty(menbeizhu)) {
            g2.drawString(menbeizhu, 730, 215+outSize4Height);
        }
//        }

        //其他商品
        g2.drawString(fanghuosuo, 30, 255+outSize4Height);
        g2.drawString(bimenqi, 30, 292+outSize4Height);
        g2.drawString(labaogai, 30, 330+outSize4Height);

        //其他商品
        g2.drawString(wujin1shu, 550, 255+outSize4Height);
        g2.drawString(wujin2shu, 550, 292+outSize4Height);
        g2.drawString(wujin3shu, 550, 330+outSize4Height);

        g2.dispose(); // 释放对象
        try {
            //ImageIO.write(bi, "JPEG", new FileOutputStream("D:/a.jpg"));// 保存图片 JPEG表示保存格式
            ImageIO.write(bi, "JPEG", new FileOutputStream(env + "/upload/ChuKuDan/" + xiaoshouChuku.getChukudanhao() + ".jpg"));// 保存图片 JPEG表示保存格式
            //xiaoshouChukuMapper.insertXiaoshouChukuIMGPath(xiaoshouChuku.getId(),loadPath+xiaoshouChuku.getChukuDanhao()+".jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return env + "/upload/ChuKuDan/" + xiaoshouChuku.getChukudanhao() + ".jpg";
    }
}
